<!DOCTYPE html>
<html class="x-admin-sm">

<head>
  <meta charset="UTF-8">
  <title>新增用户</title>
  <meta name="新增用户" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport"
    content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
  <link rel="stylesheet" href="../../css/font.css">
  <link rel="stylesheet" href="../../css/xadmin.css">
  <script type="text/javascript" src="../../lib/layui/layui.js" charset="utf-8"></script>
  <script type="text/javascript" src="../../js/xadmin.js"></script>

  <!-- 引入公用JavaScript -->
  <script src="../../js/jquery.min.js"></script>
  <script src="../../js/app.js"></script>


  <!-- my JavaScript -->
  <script type="text/javascript" src="./user-data.js"></script>
</head>

<body>
  <div class="layui-fluid">
    <div class="layui-row">
      <form class="layui-form" id="FORM">
        <div class="layui-form-item">
          <label for="workId" class="layui-form-label">
            <span class="x-red">*</span>工号</label>
          <div class="layui-input-inline">
            <input type="text" id="workId" name="workId" required="" lay-verify="workId" autocomplete="off"
              class="layui-input"></div>
        </div>
        <div class="layui-form-item">
          <label for="username" class="layui-form-label">
            <span class="x-red">*</span>姓名</label>
          <div class="layui-input-inline">
            <input type="text" id="username" name="username" required="" lay-verify="nikename" autocomplete="off"
              class="layui-input"></div>
        </div>
        <div class="layui-form-item">
          <label for=sex" class="layui-form-label">
            <span class="x-red">*</span>性别</label>
          <div class="layui-input-inline">
            男&nbsp;&nbsp;<input type="radio" name="sex" value="1" class="layui-input">
            女&nbsp;&nbsp;<input type="radio" name="sex" value="0" class="layui-input">
          </div>
        </div>

        <div class="layui-form-item">
          <label for="age" class="layui-form-label">
            <span class="x-red">*</span>年龄</label>
          <div class="layui-input-inline">
            <input type="text" id="age" name="age" required="" lay-verify="age" autocomplete="off" class="layui-input"
              onKeyUp="this.value=this.value.replace(/[^\.\d]/g,'');this.value=this.value.replace('.','');">
          </div>
        </div>
        <div class="layui-form-item">
          <label for="ID" class="layui-form-label">
            <span class="x-red">*</span>身份证号</label>
          <div class="layui-input-inline">
            <input type="text" id="ID" name="ID" required="" lay-verify="identity" autocomplete="off"
              class="layui-input" onKeyUp="">
          </div>
        </div>
        <div class="layui-form-item">
          <label for="password" class="layui-form-label">
            <span class="x-red">*</span>密码</label>
          <div class="layui-input-inline">
            <input type="password" id="password" name="password" required="" lay-verify="password" autocomplete="off"
              class="layui-input"></div>
        </div>
        <div class="layui-form-item">
          <label for="repassword" class="layui-form-label">
            <span class="x-red">*</span>确认密码</label>
          <div class="layui-input-inline">
            <input type="password" id="repassword" name="repassword" required="" lay-verify="repassword"
              autocomplete="off" class="layui-input"></div>
        </div>
        <div class="layui-form-item">
          <label for="usertype" class="layui-form-label">
            <span class="x-red">*</span>用户归属</label>
          <div class="layui-input-inline">
            <select name="usertype" id="usertype">
              <!-- 用户归属列表数据区域 -->
            </select>
          </div>
        </div>
        <div class="layui-form-item" style="display: flex;justify-content: flex-start;margin-left: 110px">
          <button id="submit" type="submit" class="layui-btn" lay-submit="" lay-filter="submit">添加</button>
        </div>

      </form>
    </div>
  </div>
  <script>
    // 从data.js中获取用户归属列表数据区域
    var usertype = $('#usertype');
    usertype.append(opitons);
    // console.log(opitons);

    layui.use(['form', 'layer'],
      function () {
        $ = layui.jquery;
        var form = layui.form,
          layer = layui.layer;

        //自定义验证规则
        form.verify({
          username: function (value) {
            if (value.length < 2) {
              return '姓名至少得2个字符啊';
            }
          },
          password: [/(.+){2,12}$/, '密码必须2到12位'],
          repassword: function (value) {
            if ($('#L_pass').val() != $('#L_repass').val()) {
              return '两次密码不一致';
            }
          },
          identity: [/(^\d{15}$)|(^\d{17}(x|X|\d)$)/, "请输入正确的身份证号"]
        });

        //监听提交
        form.on('submit(submit)', function (data) {
          // console.log(JSON.stringify(data.field))
          var age = $('#age').val();
          var password = $('#password').val();
          var repassword = $('#repassword').val();
          var pid = null;
          var roleId = $('#usertype option:selected').val();
          var salt = null;
          var sex = $('input[name="sex"]:checked').val();
          var signIn = null;
          var userId = null;
          var username = $('#username').val();
          var workId = $('#workId').val();
          var id = $('#ID').val();
          if (repassword != password) {
            layer.msg('密码前后不一致');
            return;
          }

          //判读是否有值为空
          var flag = true;
          $('#FORM input').each(function () {
            if (this.value == "") {
              flag = false
            }
          });
          if (!flag) {
            layer.msg('有参数为空');
            return;
          }


          // Ajax用于提交表单数据给后台
          $.ajax({
            url: app.ServerUrl + 'v1/user/addUser?age=' + age + '&managerId=' + managerId +
              '&password=' + password + '&pid=' + pid + '&roleId=' + roleId + '&id=' + id + '&salt=' + salt +
              '&sex=' + sex + '&userId=' + userId + '&username=' + username + '&workId=' + workId,
            type: 'post',
            dataType: 'json',
            async: false,
            data: {},
            headers: {
              "Context-Type": "application/x-www-form-urlencode"
            },
            success: function (responseData) {
              if (responseData.resultStatus == 1) {
                layer.alert("增加成功", {
                    icon: 6
                  },
                  function () {
                    // console.log(window.name);
                    //关闭当前frame
                    xadmin.close();

                    // 可以对父窗口进行刷新 
                    xadmin.father_reload();
                  });
                return false;
              } else {
                // console.log(responseData.resultIns);
                layer.msg(responseData.resultIns, {icon: 5})
              }

            },
            error: function () {
              layer.msg('添加失败', {
                icon: 5,
                time: 1000
              })
            }
          })
          return false;
        })
      });
  </script>

  <script>
    var _hmt = _hmt || [];
    (function () {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?b393d153aeb26b46e9431fabaf0f6190";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>

</body>

</html>